/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminLogin.java
 *
 * Created on Sep 11, 2010, 12:05:15 AM
 */

package Java;
/**
 *
 * @author shyan
 */

    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    public class EMPLOYEE_LOGIN extends javax.swing.JFrame {
    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "HR";
    private String userPW = "hr";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;

    /** Creates new form AdminLogin */
    public EMPLOYEE_LOGIN()
    {
        initComponents();
        setLocationRelativeTo(null);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
        alert = new javax.swing.JLabel();
        ID_field = new javax.swing.JTextField();
        PW_field = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 300));
        setResizable(false);

        jLabel3.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        jLabel3.setText("Password     :"); // NOI18N
        jLabel3.setBounds(80, 250, 120, 30);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        jLabel2.setText("Employee ID :");
        jLabel2.setBounds(80, 210, 120, 30);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        loginButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        loginButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        loginButton.setText("LOGIN");
        loginButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        loginButton.setBounds(390, 340, 90, 30);
        jLayeredPane1.add(loginButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        alert.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13)); // NOI18N
        alert.setForeground(new java.awt.Color(255, 51, 51));
        alert.setBounds(80, 290, 300, 20);
        jLayeredPane1.add(alert, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ID_field.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        ID_field.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ID_fieldMousePressed(evt);
            }
        });
        ID_field.setBounds(200, 210, 160, 25);
        jLayeredPane1.add(ID_field, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PW_field.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        PW_field.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PW_fieldMousePressed(evt);
            }
        });
        PW_field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PW_fieldActionPerformed(evt);
            }
        });
        PW_field.setBounds(200, 250, 160, 26);
        jLayeredPane1.add(PW_field, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jButton1.setText("BACK");
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(290, 340, 90, 30);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("DejaVu Serif Condensed", 3, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("LIFE RESTAURANT");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel1.setBounds(50, 60, 400, 100);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jLabel4.setBounds(0, 0, 500, 400);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed

        String employeeID=ID_field.getText();
        String password=PW_field.getText();

        if(employeeID.equals("") && password.equals(""))
        {
            alert.setText("Please type your ID and password");
        }
        else if(password.equals(""))
        {
            alert.setText("Please type your password");
        }
        else if(employeeID.equals(""))
        {
            alert.setText("Please type your ID.");
        }
        
        else
        {
            try
            {
                DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
                //Class.forName("oracle.jdbc.OracleDriver");
                con=DriverManager.getConnection(url,userID, userPW);
                stmt=con.createStatement();
                rset=stmt.executeQuery("SELECT EMP_ID,EMP_NAME,EMP_POSITION FROM EMPLOYEE "
                        + "WHERE EMP_ID = '"+employeeID+"' "
                        + "AND EMP_PW = '"+password+"'");

                if(rset.next())
                {
                    String id = employeeID;
                    String name    =rset.getString("EMP_NAME");
                    String position=rset.getString("EMP_POSITION");
                    this.setVisible(false);
                    new EMPLOYEE_MENU(id,name,position).setVisible(true);
                    //System.out.println("position -->"+ position);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Invalid user ID or Password");
                    PW_field.setText("");
                    ID_field.setText("");
                }

            }
            catch(Exception ex)
            {
                //alert.setText("Invalid user ID or Password");
                //JOptionPane.showMessageDialog(null, "Invalid user ID or Password");
                //PW_field.setText("");
                //ID_field.setText("");
                //System.err.print("ClassNotFoundException: ");
            }
        }
    }//GEN-LAST:event_loginButtonActionPerformed

    private void PW_fieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PW_fieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PW_fieldActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        new FIRST_SCREEN().setVisible(true);
        this.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void ID_fieldMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ID_fieldMousePressed
        alert.setText("");

    }//GEN-LAST:event_ID_fieldMousePressed

    private void PW_fieldMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PW_fieldMousePressed
        alert.setText("");
    }//GEN-LAST:event_PW_fieldMousePressed

    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EMPLOYEE_LOGIN().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ID_field;
    private javax.swing.JPasswordField PW_field;
    private javax.swing.JLabel alert;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JButton loginButton;
    // End of variables declaration//GEN-END:variables

}
